﻿using Groep2TINGHService;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1.AdminView
{
    /// <summary>
    /// Interaction logic for Contracten.xaml
    /// </summary>
    public partial class Contracten : MetroWindow
    {
        public Contracten()
        {
            InitializeComponent();
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            Company company = new Company();
            List<Company> lijstCompanies = myClient.GetAllCompanies();
            lijstCompanies.Add(company);
            foreach (Company r in lijstCompanies)
            {
                lstCompany.Items.Add(r.Name);
            }
            ServiceReference1.Service1Client myClient2 = new ServiceReference1.Service1Client();
            ContractFormula formula = new ContractFormula();
            List<ContractFormula> lijstFormula = myClient2.GetAllContractFormula();
            lijstFormula.Add(formula);
            foreach (ContractFormula r in lijstFormula)
            {
                cmbFormula.Items.Add(r.Description);
            }
            txtEinddatum.IsEnabled = false;
        }

        private void lstCompany_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
        }

        private void btnBevestig_Click(object sender, RoutedEventArgs e)
        {
             //controle of datum juist is
            if (Convert.ToDateTime(dtpStartDate.SelectedDate) < DateTime.Today)
            {
                MessageBox.Show("Startdatum kan niet in het verleden zijn.");
            }
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();

            Contract contract = new Contract(); //Omdat ik hier de startdatum nodig heb van contract.
            ContractFormula formula = new ContractFormula();
            Company company = new Company();
            contract.StartDate = Convert.ToDateTime(dtpStartDate.SelectedDate);//Omdat ik hier de startdatum nodig heb van contract.
            formula.Description = cmbFormula.SelectedItem.ToString();//omdat ik hier de description nodig heb voor die select in insert
            contract.EndDate = Convert.ToDateTime(txtEinddatum.Text);//omdat ik hier die einddatum van contract nodig heb
            company.Name = lstCompany.SelectedItem.ToString();//omdat ik hier de naam nodig heb voor die select in de insert

            myClient.InsertContract(contract,formula,company);
            
            
            

   
        }

        private void cmbFormula_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            ContractFormula formula = new ContractFormula();
            formula.Description = cmbFormula.SelectedItem.ToString();
            ContractFormula f2 = new ContractFormula();
            f2 = myClient.Get_ContractDurationByDescription(formula);
            DateTime StartDatum = Convert.ToDateTime(dtpStartDate.SelectedDate);
            txtEinddatum.Text = Convert.ToString(StartDatum.AddMonths(Convert.ToInt32(f2.PeriodInMonths.ToString())));
        }
    }
}
